<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/28
 * Time: 11:04
 */
namespace app\web\controller;

use app\web\service\ShopService;

class Shop extends Base{

    /**
     * @todo 当前店铺信息
     * @author ZJF
     */
    public function getShopInfo(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        $shop_service = new ShopService();
        $res = $shop_service->getShop($shop_id,$user);
        if(!empty($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }
    /**
     * @todo 关联多店铺
     * @author ZJF
     */
    public function relationShop(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        $login_id = request()->param("login_id",'');//要关联店铺的登录名
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        $shop_service = new ShopService();
        $res = $shop_service->relationShop($shop_id,$user,$login_id);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 切换店铺
     * @author ZJF
     */
    public function switchShop(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        $relation_shop_id = request()->param("relation_shop_id",0);//要切换至店铺ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        if(empty($relation_shop_id) || !is_numeric($relation_shop_id)){
            return json_ok([],1,'请选择要切换的店铺');
        }
        $shop_service = new ShopService();
        $res = $shop_service->switchShop($shop_id,$user,$relation_shop_id);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok(array('token'=>$res));
    }

    /**
     * @todo 获取关联店铺列表
     * @author ZJF
     */
    public function getRelationShop(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        $shop_service = new ShopService();
        $res = $shop_service->getRelationShop($shop_id,$user);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }
}